package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.OceanTemplateBiz;
import com.egao.orderB.entity.OceanTemplate;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-10-19 14:30:21
 * @version ：1.0
 */
@Repository
public class OceanTemplateBizImpl extends BaseDao implements OceanTemplateBiz {

	@Override
	public JsonResult list(PageResult2<T> page, OceanTemplate oceanTemplate, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanTemplate> rm = new BeanPropertyRowMapper<OceanTemplate>(OceanTemplate.class);
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id");
    sql.append(",o.customer,o.box_type");
		sql.append(",o.port_of_destination,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_template o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(oceanTemplate)){

			if(Utils.isNotEmpty(oceanTemplate.getBoxType())){
				sql.append(" and instr(o.box_type,:box_type)>0 ");
				map.put("box_type", oceanTemplate.getBoxType());
			}
			if(Utils.isNotEmpty(oceanTemplate.getPortOfDestination())){
				sql.append(" and instr(o.port_of_destination,:port_of_destination)>0 ");
				map.put("port_of_destination", oceanTemplate.getPortOfDestination());
			}
      if(Utils.isNotEmpty(oceanTemplate.getSort())&&Utils.isNotEmpty(oceanTemplate.getOrder())){
        sql.append(" order by o."+oceanTemplate.getSort()+" "+oceanTemplate.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OceanTemplate> list(OceanTemplate oceanTemplate, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanTemplate> rm = new BeanPropertyRowMapper<OceanTemplate>(OceanTemplate.class);
    StringBuffer sql = new StringBuffer("select o.id,o.organization_id");
    sql.append(",o.customer,o.box_type");
		sql.append(",o.port_of_destination,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_template o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(oceanTemplate)){
			if(Utils.isNotEmpty(oceanTemplate.getBoxType())){
				sql.append(" and instr(o.box_type,:box_type)>0 ");
				map.put("box_type", oceanTemplate.getBoxType());
			}
			if(Utils.isNotEmpty(oceanTemplate.getPortOfDestination())){
				sql.append(" and instr(o.port_of_destination,:port_of_destination)>0 ");
				map.put("port_of_destination", oceanTemplate.getPortOfDestination());
			}
      if(Utils.isNotEmpty(oceanTemplate.getSort())&&Utils.isNotEmpty(oceanTemplate.getOrder())){
        sql.append(" order by o."+oceanTemplate.getSort()+" "+oceanTemplate.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OceanTemplate getById(OceanTemplate oceanTemplate) {
		RowMapper<OceanTemplate> rm = new BeanPropertyRowMapper<OceanTemplate>(OceanTemplate.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id");
    sql.append(",o.customer,o.box_type");
		sql.append(",o.port_of_destination,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_template o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 and o.id=:id ");
		map.put("id", oceanTemplate.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(OceanTemplate oceanTemplate, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_ocean_template (");
    sql.append("organization_id,customer");
		sql.append(",box_type,port_of_destination,create_user,create_time) values ( ");
    sql.append(":organization_id,:customer");
		sql.append(",:box_type,:port_of_destination,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("customer", oceanTemplate.getCustomer());
    map.put("box_type", oceanTemplate.getBoxType());
    map.put("port_of_destination", oceanTemplate.getPortOfDestination());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(OceanTemplate oceanTemplate, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ocean_template set ");
    sql.append("customer=:customer ");
		sql.append(",box_type=:box_type,port_of_destination=:port_of_destination ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("customer", oceanTemplate.getCustomer());
    map.put("box_type", oceanTemplate.getBoxType());
    map.put("port_of_destination", oceanTemplate.getPortOfDestination());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", oceanTemplate.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(OceanTemplate oceanTemplate, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(oceanTemplate.getId().length()>0){
			for(String ID : oceanTemplate.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_ocean_template set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
